package cn.kgc.exceptions;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();

        // 判断不同的异常，跳转到不同的页面或者现实显示不同的提示信息
        if (e instanceof NullPointerException) {
            mv.setViewName("error1");
            mv.addObject("msg", "您上传的文件太大了！！");
        } else if (e instanceof RuntimeException) {
            mv.setViewName("error2");
            mv.addObject("msg", "抛出了运行时异常！！");
        }
        return mv;
    }
}
